package com.bnlr.lrparking.appui.bank;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.approve.ApproveActivity;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.MeBankCardAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.model.ApproveMessageM;
import com.bnlr.lrparking.appui.bank.model.BankCardM;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by hp on 2017/9/1.
 * 银行卡列表
 */
public class BankCardActivity extends BaseActivity {


    private TextView tvNavTitle;
    private ListView lv_bankcard;
    private MeBankCardAdapter adapter;
    private List<BankCardM.DataBean> list=new ArrayList<>();
    private ImageView iv_nav_right;
    private LinearLayout noData;
    private RelativeLayout relativeLayout ;
    private String isRealNameAuth="",realName;
    private Unbinder unbinder;
    private CarCommon carCommon = new CarCommon();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_bankcard);
        unbinder = ButterKnife.bind(this);
        initView();
    }
    @Override
    protected void onResume() {
        super.onResume();
        getBandCard();
    }

    private void initView(){
        isRealNameAuth=AppConfig.getInstance().getString("isRealNameAuth", "");
        realName = AppConfig.getInstance().getString("realName", "");
        iv_nav_right=(ImageView)findViewById(R.id.iv_nav_right);
        iv_nav_right.setImageResource(R.mipmap.add_black);
        iv_nav_right.setOnClickListener(this);
        tvNavTitle=(TextView)findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("银行卡");
        lv_bankcard=(ListView)findViewById(R.id.lv_bankcard);
        noData = (LinearLayout) findViewById(R.id.no_bank_data);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl_add_bank);
        relativeLayout.setOnClickListener(this);
        adapter = new MeBankCardAdapter(baseContext, list);
        lv_bankcard.setAdapter(adapter);
        lv_bankcard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BankCardActivity.this,BankCardDetailActivity.class);
                intent.putExtra("bank_detail",list.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_nav_right:
            case R.id.rl_add_bank:
                if (!isRealNameAuth.equals("1")) {
                    resultApprove();
                } else {
                    if (!TextUtils.isEmpty(realName)) {
                        startActivity(new Intent(BankCardActivity.this, AddBankCardOneActivity.class)
                                .putExtra("real_name", realName));
                    }
                }
                break;
            default:
                break;
        }
    }

    private void getBandCard(){
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "GetUserBank");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<BankCardM>(baseContext, true, BankCardM.class) {
                        @Override
                        public void doWork(BankCardM data, String code) {
                            if (code.equals("1")) {
                                if (data.getData() !=null && data.getData().size() != 0) {
                                    noData.setVisibility(View.GONE);
                                    list = data.getData();
                                    adapter = new MeBankCardAdapter(baseContext, data.getData());
                                    lv_bankcard.setAdapter(adapter);
                                } else {
                                    list.clear();
                                    noData.setVisibility(View.VISIBLE);
                                    adapter = new MeBankCardAdapter(baseContext, list);
                                    lv_bankcard.setAdapter(adapter);
                                }
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                if (!code.equals("1")) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }
    private void resultApprove(){
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "GetRealNameAuthStatus");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<ApproveMessageM>(this, true, ApproveMessageM.class) {
                        @Override
                        public void doWork(ApproveMessageM data, String code) {
                            if (TextUtils.equals("1", code)) {
                                isRealNameAuth=data.getData().getIsRealNameAuth();
                                AppConfig.getInstance().putString("isRealNameAuth", data.getData().getIsRealNameAuth());//用户名
                                if (isRealNameAuth.equals("0")) {
                                    carCommon.setClickSureListener(new CarCommon.ClickSureListener() {
                                        @Override
                                        public void setSure() {
                                            startActivity(ApproveActivity.class);
                                        }
                                    });
                                    carCommon.CommonDialog(BankCardActivity.this,"请您先去进行实名认证,再添加银行卡","温馨提示");
                                }else if (isRealNameAuth.equals("2")){
                                    carCommon.ReminderDialog(BankCardActivity.this,"身份信息正在审核中，请稍候再试",null);
                                } else if (isRealNameAuth.equals("3")) {
                                    carCommon.ReminderDialog(BankCardActivity.this,"您的实名认证未通过，请重新在个人中心认证",null);
                                } else {
                                    startActivity(new Intent(BankCardActivity.this,AddBankCardOneActivity.class)
                                        .putExtra("real_name",data.getData().getName()));
                                }
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
